#--------------------------------------------------------------------------------------------------
#Script for compile Yatak Operation System
#Author : Ilya Churaev
#Started : 4 May 2012	Updated : 28 Jan 2013
#File Name : makefile   Purpose : write Yatak on disk
#--------------------------------------------------------------------------------------------------

#CONSTANT
BIN_KERNEL = kernel.bin
DISK_INITRD = initrd.img

#DIRECTORIES
GRUB_DIR = GRUB
CD_DIR = CD
OS_DIR = OS
TEMP_CD = cdrom
FILES_DIR = files

#IMAGES
CD_IMAGE = $(CD_DIR)/yatak.iso

#OTHER FILES
GRUB_CONF = $(GRUB_DIR)/grub.cfg
GRUB_BACKGROUND = $(GRUB_DIR)/yatak_boot.tga
GRUB_FONT = $(GRUB_DIR)/8x13.pf2

GRUB_PATH_CD = $(TEMP_CD)/boot/grub
GRUB_CONF_CD = $(GRUB_PATH_CD)/grub.cfg
GRUB_BACKGROUND_CD = $(GRUB_PATH_CD)/yatak_boot.tga
GRUB_FONT_CD = $(GRUB_PATH_CD)/8x13.pf2

PATH_INITRD = $(TEMP_CD)/initrd
PATH_KERNEL = $(TEMP_CD)/root

ifeq "$(MAKEFLAGS)" "DEBUG"
	KERNEL = $(OS_DIR)/debug/$(BIN_KERNEL)
else
	KERNEL = $(OS_DIR)/release/$(BIN_KERNEL)
endif

INITRD = $(OS_DIR)/disk_initrd/$(DISK_INITRD)

KERNEL_CD = $(PATH_KERNEL)/$(BIN_KERNEL)
INITRD_CD = $(PATH_INITRD)/$(DISK_INITRD)

QEMU_MEMURY = 128

PATH_LOADER_CD = boot/grub/core.img

.PHONY: clean

all:
	@echo "#######################################################################"
	@echo "# Welcome to make file for Yatak compiling                            #"
	@echo "#    Version: 2.6.1                                                   #"
	@echo "#    Last update: 28 Jan 2013                                         #"
	@echo "#    Author: Ilya Churaev (ilyachur@gmail.com)                        #"
	@echo "#######################################################################"
	@echo "Start compiling kernel..."
	@make -C $(OS_DIR) MAKEFLAGS=$(MAKEFLAGS)
	@echo "Kernel compiled...OK"
	@echo "Creating cd image..."
	@make create_cd	MAKEFLAGS=$(MAKEFLAGS)
	@echo "CD created...OK"
	
debug:
	@echo "Debug mode..."
	@make MAKEFLAGS=DEBUG

install:
	@make play
	
debug_play:
	@make debug
	@echo "Start virtual mashine..."
	@qemu-system-x86_64 -s -S -m $(QEMU_MEMURY) -cdrom $(CD_IMAGE) -serial stdio &
	@echo "Start ddd..."
	@ddd
	
play:
	@make
	@echo "Running..."
	qemu-system-x86_64 -m $(QEMU_MEMURY) -cdrom $(CD_IMAGE) -serial stdio
	
create_cd:
	@echo "    Create directory for cd image..."
	@test -d $(CD_DIR) || mkdir $(CD_DIR)
	@echo "    Remove old cd image..."
	@test ! -w $(CD_IMAGE) || rm $(CD_IMAGE)
	@echo "    Create temp directory for files which will write on image..."
	@test -d $(TEMP_CD) || mkdir $(TEMP_CD)
	@echo "    Create directory for GRUB..."
	@test ! -d $(TEMP_CD)/boot || rm -R $(TEMP_CD)/boot
	@mkdir $(TEMP_CD)/boot
	@mkdir $(TEMP_CD)/boot/grub
	@echo "    Copy grub.cfg on image..."
	@if [ -w $(GRUB_CONF) ]; then if [ -w $(GRUB_CONF_CD) ]; then rm $(GRUB_CONF_CD) ; fi ; cp -R $(GRUB_CONF) $(GRUB_CONF_CD) ; fi
	@echo "    Copy grub background on image..."
	@if [ -w $(GRUB_BACKGROUND) ]; then if [ -w $(GRUB_BACKGROUND_CD) ]; then rm $(GRUB_BACKGROUND_CD) ; fi ; cp -R $(GRUB_BACKGROUND) $(GRUB_BACKGROUND_CD) ; fi
	@echo "    Copy grub font on image..."
	@if [ -w $(GRUB_FONT) ]; then if [ -w $(GRUB_FONT_CD) ]; then rm $(GRUB_FONT_CD) ; fi ; cp -R $(GRUB_FONT) $(GRUB_FONT_CD) ; fi
	@echo "    Copy directory for kernel..."
	@test -d $(PATH_KERNEL) || mkdir $(PATH_KERNEL)
	@echo "    Remove old kernel..."
	@if [ -w $(KERNEL_CD) ]; then rm $(KERNEL_CD) ; fi
	@echo "    Copy kernel..."
	@cp -R $(KERNEL) $(KERNEL_CD)
	@echo "    Copy directory for initrd..."
	@test -d $(PATH_INITRD) || mkdir $(PATH_INITRD)
	@echo "    Remove old initrd image..."
	@if [ -w $(INITRD_CD) ]; then rm $(INITRD_CD) ; fi
	@echo "    Copy initrd image..."
	@cp -R $(INITRD) $(INITRD_CD)
	@echo "    Copy files on cd..."
	@test ! -d $(FILES_DIR) || cp -R $(FILES_DIR)/* $(TEMP_CD)
	@echo "    Creating cd image..."
	@grub-mkrescue -o $(CD_IMAGE) $(TEMP_CD)
	#@mkisofs -b $(PATH_LOADER_CD) -no-emul-boot -boot-load-size 4 -boot-info-table -iso-level 3 -r -J -publisher "Legos (<!-- m --><a href=\"http://vk.com/churaev_ilya\">http://vk.com/churaev_ilya</a><!-- m -->)" -o $(CD_IMAGE)  $(TEMP_CD)
	@echo "    Remove temp..."
	@rm -R $(TEMP_CD)
	
clean:
	@make -C $(OS_DIR) clean
	@test ! -d $(PATH_INITRD) || rm -R $(PATH_INITRD)
	@test ! -d $(TEMP_CD) || rm -R $(TEMP_CD)
	@test ! -f $(INITRD) || rm -R $(INITRD)	
